/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.tables;

/**
 *
 * @author Tri
 */
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import database.DBManager;
import database.objects.DOUser;
import sun.java2d.DefaultDisposerRecord;

public class DTUser {
   private DB m_db;
   public DTUser() {
       m_db = DBManager.getInstance().getDB();
   }
   
   public DOUser getUserInfo(String userID) {
       DOUser userInfo = null;
       BasicDBObject searchQuery = new BasicDBObject().append(DOUser.KEY_USER_ID, userID);
       DBCursor cursor = getCollection().find(searchQuery);
       if(cursor.count() > 0) {
           BasicDBObject result = (BasicDBObject)cursor.next();
           String usernme = result.getString(DOUser.KEY_USERNAME);
           double money = result.getDouble(DOUser.KEY_MONEY);
           int currentLevel = result.getInt(DOUser.KEY_CURRENT_LVL);
           int latestLevel = result.getInt(DOUser.KEY_LATEST_LVL);
           long timeToGainTime = result.getLong(DOUser.KEY_TIME_TO_GAIN_LIVE);
           int lives = result.getInt(DOUser.KEY_LIVES);
           String avatarURL = result.getString(DOUser.KEY_AVATAR);
           // BasicDBList list = (BasicDBList)cursor.curr().get(DOMap.KEY_OBSTACLE_LIST);
           BasicDBList listFriends = (BasicDBList)cursor.curr().get(DOUser.KEY_FRIENDS_LIST);
           int i = 0;
           String[] friendsList = new String[listFriends.size()];
           for (Object tempObj : listFriends) {
               BasicDBObject tempObject = (BasicDBObject)tempObj;
               String friendID = tempObject.getString(DOUser.KEY_SUB_FRIENDS);
               friendsList[i] = friendID;
               i++;
           }
           
           BasicDBList listItems = (BasicDBList)cursor.curr().get(DOUser.KEY_ITEMS_LIST);
           i = 0;
           String[] itemsList = new String[listItems.size()];
           for (Object tempObj : listItems) {
               BasicDBObject tempObject = (BasicDBObject)tempObj;
               String itemID = tempObject.getString(DOUser.KEY_SUB_ITEMS);
               itemsList[i] = itemID;
               i++;
           }
           
           userInfo = new DOUser(userID, usernme, money, currentLevel, latestLevel, timeToGainTime, lives, friendsList, itemsList);
           userInfo.setAvatar(avatarURL);
       }
       return  userInfo;
   }
   
   public void insertUser(DOUser user) {
       BasicDBObject dbObject = new BasicDBObject();
       dbObject.put(DOUser.KEY_USER_ID, user.getUserID());
       dbObject.put(DOUser.KEY_USERNAME, user.getUsername());
       dbObject.put(DOUser.KEY_AVATAR, user.avatarURL);
       dbObject.put(DOUser.KEY_CURRENT_LVL, user.currentLevel);
       dbObject.put(DOUser.KEY_LATEST_LVL, user.latestLevel);
       dbObject.put(DOUser.KEY_LIVES, user.lives);
       dbObject.put(DOUser.KEY_TIME_TO_GAIN_LIVE, user.timeToGainTime);
       for (String s : user.getFriendsList()) {
           BasicDBObject tempObject = new BasicDBObject();
           tempObject.put(DOUser.KEY_SUB_FRIENDS, s);
           dbObject.put(DOUser.KEY_FRIENDS_LIST, tempObject);
       }
       
       for (String s : user.getItemsList()) {
           BasicDBObject tempObject = new BasicDBObject();
           tempObject.put(DOUser.KEY_SUB_ITEMS, s);
           dbObject.put(DOUser.KEY_ITEMS_LIST, tempObject);
       }
       getCollection().insert(dbObject);
   }
   
   public boolean deleteUser(String userID) {
       BasicDBObject dbObject = new BasicDBObject().append(DOUser.KEY_USER_ID, userID);
       DBCursor cursor = getCollection().find(dbObject);
       if(cursor.count() > 0) {
           getCollection().remove(cursor.curr());
           return true;
       }
       return false;
   }
   
   public void updateUser(DOUser user) {
       BasicDBObject searchQuery = new BasicDBObject().append(DOUser.KEY_USER_ID, user.getUserID());
       BasicDBObject queryResult = new BasicDBObject();
       queryResult.put(DOUser.KEY_USERNAME, user.getUsername());
       queryResult.put(DOUser.KEY_AVATAR, user.avatarURL);
       queryResult.put(DOUser.KEY_CURRENT_LVL, user.currentLevel);
       queryResult.put(DOUser.KEY_LATEST_LVL, user.latestLevel);
       queryResult.put(DOUser.KEY_LIVES, user.lives);
       queryResult.put(DOUser.KEY_TIME_TO_GAIN_LIVE, user.timeToGainTime);
       for (String s : user.getFriendsList()) {
           BasicDBObject tempObject = new BasicDBObject();
           tempObject.put(DOUser.KEY_SUB_FRIENDS, s);
           queryResult.put(DOUser.KEY_FRIENDS_LIST, tempObject);
       }
       for (String s : user.getItemsList()) {
           BasicDBObject tempObject = new BasicDBObject();
           tempObject.put(DOUser.KEY_SUB_ITEMS, s);
           queryResult.put(DOUser.KEY_ITEMS_LIST, tempObject);
       }
       getCollection().update(searchQuery, queryResult);
   }
   
   public DBCollection getCollection() {
       return m_db.getCollection("User");
   }
}
